package com.gitee.sop.order.pay.base.ali;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.gitee.sop.order.entity.OrderForm;
import com.gitee.sop.order.pay.base.PayBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @author Admin
 */
@Component
public class AliPayUtil {

    @Autowired
    private PayBaseService payBaseService;

    /**
     * 创建支付签证
     *
     * @param order 订单信息
     * @return 订单签证信息，客户端可直接使用
     * @throws AlipayApiException
     */
    public String[] orderCreate(OrderForm order) throws AlipayApiException {
        AliPayConfig aliPayConfig = payBaseService.getPayConfig(AliPayConfig.class, "ali");
        String appId = aliPayConfig.getAppId();
        String appPrivateKey = aliPayConfig.getAppPrivateKey();
        String alipayPublicKey = aliPayConfig.getAliPayPublicKey();
        AlipayClient alipayClient =
                new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                        appId,
                        appPrivateKey,
                        "json", "utf-8",
                        alipayPublicKey,
                        "RSA2");
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(order.getPayDescription());
        model.setSubject(order.getPayDescription());
        model.setOutTradeNo(order.getOrderNumber());
        model.setTimeoutExpress("30m");
        BigDecimal totalPrice = order.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_DOWN);
        model.setTotalAmount(totalPrice.toString());
        model.setProductCode("QUICK_MSECURITY_PAY");
        request.setNotifyUrl(aliPayConfig.getNotifyUrl());
        request.setBizModel(model);
        AlipayTradeAppPayResponse response;
        try {
            //这里和普通的接口调用不同，使用的是sdkExecute
            response = alipayClient.sdkExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            throw e;
        }
        //就是orderString 可以直接给客户端请求，无需再做处理。
        String[] arr = new String[2];
        arr[0] = response.getBody();
        arr[1] = response.getTradeNo();
        return arr;
    }


    /**
     * 校验签名
     *
     * @param params
     * @return
     * @throws AlipayApiException
     */
    public boolean checkSign(Map<String, String> params) throws AlipayApiException {
        return AlipaySignature.rsaCheckV1(params, payBaseService.getPayConfig(AliPayConfig.class, "ali").getAliPayPublicKey(), "utf-8", "RSA2");
    }


}
